package channel;

import java.io.InputStream;
import java.util.Date;
import java.util.List;

import stream.Stream;
import weibo4j.Status;
import weibo4j.Weibo;
import weibo4j.WeiboException;


public class ChannelWeibo implements Channel {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	
	static public final int TYPE = WEIBO;
	
	public static void main(String[] args) throws WeiboException, InterruptedException {
		ChannelWeibo channelWeibo = new ChannelWeibo();
		channelWeibo.setWeiboUsr("huang_xinxi@yeah.net");
		channelWeibo.setPassword("abcdef");

		channelWeibo.setStatus("Hesdflljo");

		channelWeibo.THAT();

//		Thread.sleep(20000);
		
//		channelWeibo.setTargetScreenName("CincyHuang");
//		System.out.println(channelWeibo.THIS());
	}
	
	private String weiboUsr;			//that
	private String password;			//that
	private String key;					//this
	private String status;				//that
	
	private String targetScreenName;	//this
	
	private Date lastCheckedTime = new Date();
	
	public ChannelWeibo() throws WeiboException {
		this.weiboUsr = TRIGGER_ADDRESS;
		this.password = TRIGGER_PASSWORD;
		this.key = "";
	}
	
	public ChannelWeibo(String targetScreenName) throws WeiboException {
		this.targetScreenName = targetScreenName;
		this.weiboUsr = TRIGGER_ADDRESS;
		this.password = TRIGGER_PASSWORD;
		this.key = "";

	}
	
	public ChannelWeibo(String targetScreenName, String key) throws WeiboException {
		this.targetScreenName = targetScreenName;
		this.weiboUsr = TRIGGER_ADDRESS;
		this.password = TRIGGER_PASSWORD;
		this.key = key;

	}
	
	public ChannelWeibo(String weiboUsr, String password,
						String status) throws WeiboException {
		this.weiboUsr = weiboUsr;
		this.password = password;
		this.status = status;
	}
	
	
	@SuppressWarnings("finally")
	@Override
	public int THIS() {
		int newWeibo = 0;
		
		try {
			newWeibo = getTargetNewWeiboCount();
			return newWeibo;
		} catch (WeiboException e) {
			newWeibo = 0;
			e.printStackTrace();
		} finally {
			return newWeibo;
		}
	}

	@Override
	public void THAT() {
		try {
			updateStatus();
		} catch (WeiboException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	private int getTargetNewWeiboCount() throws WeiboException {
		
		int targetNewWeiboCount = 0;

		Date now = new Date();
		List<Status> status = Weibo.getInstance(weiboUsr, password).
									getUserTimeline(targetScreenName);
		
		for (Status s : status) {
			Date sendDate = s.getCreatedAt();
		
			if ( sendDate.after(lastCheckedTime) &&
				 s.getText().contains(key) ) {
				targetNewWeiboCount++;
			}
		}
		lastCheckedTime = now;
		
		return targetNewWeiboCount;
	}
	
	private void updateStatus() throws WeiboException {
		
		Weibo.getInstance(weiboUsr, password).updateStatus(status);
	
	}
	
	public void setWeiboUsr(String weiboUsr) {
		this.weiboUsr = weiboUsr;
	}
	
	public void setPassword(String password) {
		this.password = password;
	}
	
	public void setStatus(String status) {
		this.status = status;
	}
	
	public void setKey(String key) {
		this.key = key;
	}

	public void setTargetScreenName(String targetScreenName) throws WeiboException {
		this.targetScreenName = targetScreenName;
	}
	
	public int getType() {
		return TYPE;
	}
	
	public InputStream toStream() {
		return Stream.getStream(this);
	}

}
